<script setup>
import GoodsItem from './component/GoodsItem.vue'
import { getCategoryAPI } from '@/api/category'
import { onMounted, ref, watch } from 'vue'
import { getBannerAPI } from '@/api/category'
import { useRoute } from 'vue-router'
import { lazyApi } from '@/utils/lazy'

//获取当前路由id数据
const categoryData = ref([])
const route = useRoute()
const getCategory = async (id = route.params.id) => {
  const res = await getCategoryAPI(id)
  categoryData.value = res.data.result
}
const { target } = lazyApi(getCategory)
// 监听路由变化
watch(
  () => route.path,
  () => {
    getCategory()
  }
)

//获取轮播图数据
const bannerList = ref([])
const getBanner = async () => {
  const res = await getBannerAPI({
    distributionSite: '2'
  })
  bannerList.value = res.data.result
}
onMounted(() => getBanner())
</script>

<template>
  <div class="top-category">
    <div class="bread-container">
      <!-- 面包 -->
      <el-breadcrumb separator=">" class="head-breadcrumb">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>{{ categoryData.name }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 轮播 -->
    <div class="home-banner">
      <el-carousel height="500px">
        <el-carousel-item v-for="item in bannerList" :key="item.id">
          <img :src="item.imgUrl" alt="" />
        </el-carousel-item>
      </el-carousel>
    </div>
    <!-- 商品一级分类 -->
    <div class="sub-list" ref="target">
      <h3>全部分类</h3>
      <ul>
        <li v-for="item in categoryData.children" :key="item.id">
          <router-link :to="`/category/sub/${item.id}`">
            <img v-lazyImg="item.picture" />
            <p>{{ item.name }}</p>
          </router-link>
        </li>
      </ul>
    </div>
    <div class="ref-goods" v-for="item in categoryData.children" :key="item.id">
      <div>
        <div class="head">
          <router-link :to="`/category/sub/${item.id}`">查看全部 ></router-link>
        </div>
        <h3>- {{ item.name }} -</h3>
      </div>
      <div class="body">
        <GoodsItem v-for="goods in item.goods" :goods="goods" :key="goods.id"></GoodsItem>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.top-category {
  background-color: #f5f5f5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding-bottom: 50px;
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
    padding: 0;
    margin: 0;
  }

  .sub-list {
    width: 1240px;
    margin-top: 20px;
    background-color: #fff;

    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;

      li {
        width: 168px;
        height: 160px;
        list-style: none;
        &:hover p {
          color: #27bb9a;
        }

        a {
          text-align: center;
          display: block;
          font-size: 16px;
          text-decoration: none;

          img {
            width: 100px;
            height: 100px;
          }

          p {
            line-height: 40px;
            color: black;
          }
        }
      }
    }
  }

  .ref-goods {
    width: 1240px;
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      width: 100%;
      display: flex;
      padding: 30px 0px 0px 1120px;
      a {
        text-decoration: none;
        color: #666;
        &:hover {
          color: #27bb9a;
        }
      }
    }

    .body {
      display: flex;
      justify-content: space-around;
      padding: 0 40px 30px;
    }
  }

  .bread-container {
    padding: 25px 0;
    width: 1220px;
  }
}
.home-banner {
  width: 1240px;
  height: 500px;
  margin: 0 auto;

  img {
    width: 100%;
    height: 500px;
  }
}
</style>
